package com.feng.annotation.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.feng.annotation.controller.UserController;
import com.feng.annotation.dao.UserDao;
import com.feng.annotation.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author Lee
 * @date 2019/3/9
 */
@EnableTransactionManagement
@Configuration
@PropertySource(value = {"classpath:db.properties"})
//@ComponentScan(value = "com.feng.annotation")
@Import({UserController.class, UserService.class, UserDao.class})
public class TxConfig {

    @Bean
    public DataSource dataSource(@Value("${druid.username}") String username,
                                 @Value("${druid.password}") String password,
                                 @Value("${druid.url}") String url,
                                 @Value("${druid.driverClass}") String driverClass){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
